﻿@page "/weather"
@attribute [StreamRendering(true)]

<PageTitle>Weather</PageTitle>

<Heading1>Weather</Heading1>

<p class="my-4">This component demonstrates showing data.</p>

@if (forecasts == null)
{
    <Loading />
}
else
{
    <DataGrid Items=@forecasts class="max-w-screen-md" TableStyle="TableStyle.StripedRows | TableStyle.UppercaseHeadings">
        <Column Field="(WeatherForecast x) => x.Date" Format="dd/MM/yyyy" />
        <Column Title="Temp. (C)" Field="(WeatherForecast x) => x.TemperatureC" />
        <Column Title="Temp. (F)" Field="(WeatherForecast x) => x.TemperatureF" />
        <Column Field="(WeatherForecast x) => x.Summary" />
    </DataGrid>
}

@code {
    private WeatherForecast[]? forecasts;

    protected override async Task OnInitializedAsync()
    {
        // Simulate asynchronous loading to demonstrate streaming rendering
        await Task.Delay(500);

        var startDate = DateOnly.FromDateTime(DateTime.Now);
        var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
        forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = startDate.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = summaries[Random.Shared.Next(summaries.Length)]
        }).ToArray();
    }

    public class WeatherForecast
    {
        public DateOnly Date { get; set; }
        public int TemperatureC { get; set; }
        public string? Summary { get; set; }
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    }
}
